Using frame and grid

Load, resize and save image with Python Imaging Library (PIL)

Optionally display images using Tkinter PIL from: http://www.pythonware.com/products/pil/

http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe

A version for 3.X will be released later.

Preparation:

  1. Unofficial Windows Binaries for Python Extension Packages https://www.lfd.uci.edu/~gohlke/pythonlibs/

  2. Download: Pillow5.2.0cp37cp37mwin_amd64.whl

  3. Install: i:\Python\Projects\gui>pip3 install Pillow-5.2.0-cp37-cp37m-win_amd64.whl

    • Output:

      • Processing i:\python\projects\gui\pillow-5.2.0-cp37-cp37m-win_amd64.whl

      • Installing collected packages: Pillow

      • Successfully installed Pillow-5.2.0

Example 1:

+---------+---------+-----------------------------+
| label1  | entry1  |                             |
+---------+---------+                             |
| label2  | enrty2  |           image             |
+---------+---------+                             |
|         |         |                             |
+---------+---------+---------+---------+---------+
| checkbutton       | button1 | button2 | button3 |
+-------------------+---------+---------+---------+

Notes:

  1. You aren’t creating a grid object. “grid” is not a thing, it’s just instructions for how to lay out a set of widgets.

  2. Another trick to learning to work with grid is to give each frame you’re trying to use in a grid a distinctive COLOR so you can see where one frame ends and another begins.

[1]:
#!/usr/bin/python3
try:
    from tkinter import *
except ImportError:
    from Tkinter import *

from PIL import Image, ImageTk

app = Tk()
app.title("Image in Grid")

root = Frame(app,
             highlightbackground = "gray",
             highlightcolor = "gray",
             highlightthickness = 2,
             bd = 0)

# root.configure(background = "gray")
# root.configure(bg='#334353')

root.pack()
root.pack_propagate(False)

app_label1 = Label(root, text = "Username").grid(row=0, sticky = W + N, pady = 8)
app_entry1 = Entry(root).grid(row=0, column=1, sticky = W + N, pady = 8)

app_label2 = Label(root, text = "Password").grid(row=1, sticky = W + N, pady = 4)
app_entry2 = Entry(root).grid(row=1, column=1, sticky = W + N, pady = 4)

root.grid_rowconfigure(2, minsize=150)
# root.grid_columnconfigure(0, minsize=100)

# Add check button
app_checkbutton1 = Checkbutton(root,
                               text = "Preserve Aspect"
                               )
app_checkbutton1.grid(row=3, column=0, columnspan=2, sticky=W)

# pick an image file you have .bmp  .jpg  .gif.  .png
# (if not in the working directory, give full path)
file_in = "_DSC9722.png"
pil_image = Image.open(file_in)

# retrieve some information
print("image.size   = (%d, %d)" % pil_image.size)
print("image.format = %s" % pil_image.format)  # 'PNG'

# common modes are
# "L" (luminance) for greyscale images,
# "RGB" for true color images,
# "CMYK" for pre-press images
print("image.mode   = %s" % pil_image.mode)    # 'RGB'

# change to a 200x100 size image using best downsize filter
image200x200 = pil_image.resize((200, 200), Image.ANTIALIAS)

# save the resized image as .jpg file to working directory
# (you could save it as .bmp, .gif, .png  too)
# file_out = file_in[:-4] + '.jpg'
# image200x200.save(file_out)
# print("File saved as %s" % file_out)

# Show image using Tkinter
# convert PIL image objects to Tkinter PhotoImage objects

# Make a tkinter-friendly image object ...
tk_image1 = ImageTk.PhotoImage(image200x200)

# tk_image2 = ImageTk.PhotoImage(pil_image)
# to display the images on labels
# label1 = Label(root, image = tk_image1).pack(padx=5, pady=5)
# label2 = Label(root, image = tk_image2).pack(padx=5, pady=5)
canvas1 = Canvas(root, width = 205, height = 205, bg='#334353')
canvas1.create_image(5, 5,
                     image = tk_image1,
                     anchor = NW)
# canvas1.configure()
canvas1.grid(row=0, column=2,
             rowspan=3, columnspan=3,
             sticky=W+E+N+S,
             padx=5, pady=5)
# canvas1.pack(fill=BOTH, expand=1)

# =====================================
# Canvas and PhotoImage - Works, but no zoom, no JPEG
# canvas1 = Canvas(root, width = 120, height = 120)
# app_image1 = PhotoImage(file = image_file)
# value(s) for the -zoom option must be positive and integer
# app_image1 = app_image1.zoom(x = -2, y = -2)
# =====================================

app_button1 = Button(root,
                     text = "Repeat",
                     )
app_button1.grid(row=3, column=2)

app_button2 = Button(root,
                     text = "Cancel",
                     )
app_button2.grid(row=3, column=3)

app_button3 = Button(root,
                     text = "OK",
                     )
app_button3.grid(row=3, column=4)

# app.mainloop()
image.size   = (1200, 1109)
image.format = PNG
image.mode   = RGB

FramesGrids